<template>
  <div>
    <el-tree
      ref="tree"
      :data="list"
      :props="{label:'name'}"
      show-checkbox
      default-expand-all
      check-strictly
      node-key="id"
    />
    <div style="text-align: right;">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="$emit('success')">取消</el-button>
    </div>
  </div>

</template>
<script>
import { getPermissionList } from '@/api/permisson'
import { tranListToTreeList } from '@/utils'
import { getRoleDetail, assignPrem } from '@/api/settings'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async  hSubmit() {
      // 获取当前的选中节点的id
      const permIds = this.$refs.tree.getCheckedKeys()
      try {
        // 发送请求
        const res = await assignPrem({ id: this.roleId, permIds: permIds })
        console.log(res)
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    // 获取数据
    async loadPermissionList() {
      // 获取权限点
      const res = await getPermissionList()
      console.log(res, 11111)
      const Perids = await getRoleDetail(this.roleId)
      console.log(Perids, 22222)
      // 使用setCheckedKeys选中角色拥有的权限点
      this.$refs.tree.setCheckedKeys(Perids.data.permIds)

      // 将平铺的数据转换为树形数据  保存数据
      this.list = tranListToTreeList(res.data)
    }
  }
}
</script>
